﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PlugNT.Cms.Model;
using PlugNT.Common;
using FJASPX.Framwork.Helper;
using System.Web.Security;
using log4net;
using FJASPX.Framwork.Config;

namespace PlugNT.Cms.Web.Personal
{
    public partial class Register : System.Web.UI.Page
    {
        private static readonly ILog logger = LogManager.GetLogger(typeof(Register));

        private PlugNT.Cms.BLL.User curUser = new PlugNT.Cms.BLL.User();
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string username = this.username.Value.Trim();

            int formCount = Request.Form.Keys.Count;
            UserInfo uinfo = new UserInfo();
            uinfo.AddField("username", this.username.Value.Trim());
            uinfo.AddField("password", this.password.Value.Trim());
            uinfo.AddField("email", this.emailname.Value.Trim());
            uinfo.AddField("sex", this.RadioButtonList1.SelectedItem.Text);
            uinfo.AddField("type", "0");
            uinfo.AddField("address", this.address.Value.Trim());
            uinfo.AddField("address1", this.address1.Value.Trim());
            uinfo.AddField("mobile", this.mobile.Value.Trim());
            uinfo.AddField("realName", this.contactperson.Value.Trim());
            uinfo.AddField("logintime", DateTime.Now);
            try
            {
                MembershipCreateStatus creatStatude;
                string creatMessage = string.Empty;
                Membership.CreateUser(this.username.Value.Trim(), this.password.Value.Trim(), DESEncrypt.Encrypt(this.emailname.Value.Trim()), "default", "default", true, out creatStatude);
                logger.Error("creatStatude:" + creatStatude);
                switch (creatStatude)
                {
                    case MembershipCreateStatus.DuplicateUserName:
                        creatMessage = "用户已经存在，请重新选择用户名";
                        break;
                    case MembershipCreateStatus.DuplicateEmail:
                        creatMessage = "该邮件地址已经注册，请重新填写邮件地址";
                        break;
                    case MembershipCreateStatus.InvalidPassword:
                        creatMessage = "密码的格式设置不正确";
                        break;
                    case MembershipCreateStatus.InvalidEmail:
                        creatMessage = "电子邮件地址的格式设置不正确";
                        break;
                    case MembershipCreateStatus.InvalidAnswer:
                        creatMessage = "密码提示问题答案的格式设置不正确";
                        break;
                    case MembershipCreateStatus.InvalidQuestion:
                        creatMessage = "密码提示问题的格式设置不正确";
                        break;
                    case MembershipCreateStatus.InvalidUserName:
                        creatMessage = "在数据库中未找到用户名";
                        break;
                    case MembershipCreateStatus.ProviderError:
                        creatMessage = "服务器出错，请稍候重试";
                        break;
                    case MembershipCreateStatus.UserRejected:
                        creatMessage = "因为提供程序定义的某个原因而未创建用户";
                        break;
                    case MembershipCreateStatus.Success:
                        Roles.AddUsersToRole(new string[] { username }, ConfigBase.GetConfig("RolePersonal"));

                        curUser.Add(uinfo);
                        FormsAuthentication.SetAuthCookie(username, false);
                        creatMessage = "恭喜，注册成功！";
                        JScript.AlertAndRedirect("注册成功！", "/Personal/Index.aspx", true);
                        break;
                    default:
                        creatMessage = "服务器端有错误发生，请稍候重试";
                        break;
                }
                JScript.Alert(creatMessage, this.Page);
            }
            catch (Exception ex)
            {
                JScript.Alert("注册失败，请重新注册！" + ex, this.Page);
            }
        }
    }
}